package cn.edu.fzu.delivery.controller.networkadmin;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.domain.entity.Courier;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.UserTypeEnum;
import cn.edu.fzu.delivery.service.CourierService;
import cn.edu.fzu.delivery.service.NetworkAdminService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 快递管理
 *
 * @author fangxin
 * @date 2022/11/15
 */
@EnableOpenApi
@Api(tags = "网点管理员-快递管理接口")
@RestController
@RequestMapping("/api/auth/networkAdmin/courier")
public class NetWorkCourierController {

    @Resource
    private NetworkAdminService networkAdminService;
    @Resource
    private CourierService      courierService;

    @ApiOperation(value = "网点管理员：查询所有快递", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @GetMapping("/listAll")
    public Result list(String courierId, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<Courier> page = PageHelper.startPage(pageNum, pageSize)) {
            List<Courier> couriers = networkAdminService.listAllCouriers(currentUser.getNetwork().getId(), courierId);
            return new Result(new PageVO<>(page.getTotal(), couriers));
        }
    }


    @ApiOperation(value = "网点管理员：查询所有待派件快递", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @GetMapping("/listDelivery")
    public Result listDelivery(String courierId, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);

        List<Courier> couriers = networkAdminService.listDeliveryCouriers(currentUser.getNetwork().getId(), courierId);
        return new Result(PageVO.page(pageNum, pageSize, couriers));
    }


    @ApiOperation(value = "网点管理员：查询快递详情", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号") })
    @RequestMapping("/detail")
    public Result detail(String courierId) {
        //这里就不做权限校验，只要有快递单号，不管是不是自己网点的都可以查询
        return new Result(courierService.getByCourierId(courierId));
    }


    @ApiOperation(value = "网点管理员：快递派件，分配快递员", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号") })
    @GetMapping("/delivery")
    public Result delivery(String courierId, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkAdminService.delivery(currentUser, courierId);
        return Result.SUCCESS;
    }
}
